function clearForm() {
    var $ = layui.$;
    $("input[name='outbookName'] ").val("");
    $("input[name='outbookOrdnum'] ").val("");
    $("input[name='outbookNeedname'] ").val("");
    $("input[name='outbookNeedaddr'] ").val("");
    $("input[name='outbookNeednum'] ").val("");
    $("input[name='outbookNum'] ").val("");
    $("input[name='workName'] ").val("");
}
(function () {
    findWork();
    var form = layui.form;
    form.verify({
        checkOutbookName: function (value) {
            if (value.length == 0) {
                return "图书名称不能为空";
            }
        },
        checkOutbookOrdnum: function (value) {
            if (value.length ==0) {
                return "出库单号不能为空";
            }
        },
        checkOutbookNeedname: function (value) {
            if (value.length ==0) {
                return "需求商不能为空";
            }
        },
        checkOutbookNeedaddr: function (value) {
            if (value.length ==0) {
                return "地址不能为空";
            }
        },
        checkOutbookNeednum: function (value) {
            if (value.length ==0) {
                return "联系电话不能为空";
            }
        },
        checkOutbookNum: function (value) {
            if (value.length ==0) {
                return "数量不能为空";
            }
        }
    });

    form.on('submit(addOutbookBtn)', function (data) {

        var $ = layui.$;
        var outbookName = data.field.outbookName;
        var outbookOrdnum =data.field.outbookOrdnum;
        var outbookNeedname =data.field.outbookNeedname;
        var outbookNeedaddr =data.field.outbookNeedaddr;
        var outbookNeednum =data.field.outbookNeednum;
        var outbookNum =data.field.outbookNum;
        var workName =data.field.workName;
        $.ajax({
            url: "../OutBook/insert",
            method: "post",
            data: {
                "outbookName": outbookName,
                "outbookOrdnum": outbookOrdnum,
                "outbookNeedname": outbookNeedname,
                "outbookNeedaddr":outbookNeedaddr,
                "outbookNeednum": outbookNeednum,
                "outbookNum": outbookNum,
                "workName": workName
            },
            success: function (data) {
                layui.layer.alert(data.msg,{icon:1});
                if (data.code == 200) {
                    layui.table.reload("demo");
                    clearForm();

                } else if (data.code == 500) {
                    var layer = layui.layer;
                    layer.alert(data.msg, {icon: 2});
                }
            },
            error: function (data) {
                console.log(data);
            }
        });
        return false;
    });
})();

function findWork(){
    var $ =layui.$;
    $.ajax({
        url: "../work/findWork",
        method:"get",
        success:function (result) {
            $("#workName").empty();
            if (result.code == 200) {
                $("#workName").append(new Option("请选择操作人员", ""));
                $.each(result.data, function (index, item) {
                    $('#workName').append(new Option(item.workName, item.workName));
                });
            } else {
                $("#workName").append(new Option("暂无数据", ""));
            }
            layui.form.render("select");
        }
    });
}